package com.dailyblue.project.estate.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dailyblue.project.estate.commons.bean.ParkingUse;
import com.dailyblue.project.estate.commons.handler.ParkingUseException;
import com.dailyblue.project.estate.commons.mapper.ParkingUseMapper;
import com.dailyblue.project.estate.commons.util.JsonResult;
import com.dailyblue.project.estate.commons.util.ResultTool;
import com.dailyblue.project.estate.service.ParkingUseService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther:Liu
 * @Testname:ParkingUseServiceImpl
 * @Date:2025/9/18 15:54
 */
@Service
public class ParkingUseServiceImpl extends ServiceImpl<ParkingUseMapper, ParkingUse> implements ParkingUseService {
    @Resource
    private ParkingUseMapper parkingUseMapper;
    @Override
    public JsonResult saveParkingUse(ParkingUse parkingUse) {
        boolean b = save(parkingUse);
        if (!b) {
            throw new ParkingUseException("添加失败");
        }
        return ResultTool.success("save ParkingUse");
    }

    @Override
    public JsonResult findOneParkingUse(String parkingUseId) {
        ParkingUse parkingUse = getById(parkingUseId);
        return ResultTool.success(parkingUse);
    }

    @Cacheable(cacheNames = "ParkingUse")
    public JsonResult findAllParkingUse() {
        List<ParkingUse> parkingUseList = list();
        return ResultTool.success(parkingUseList);
    }

    @CacheEvict(cacheNames = "ParkingUse", allEntries = true)
    public JsonResult deleteParkingUse(String parkingUseId) {
        boolean b = removeById(parkingUseId);
        if (!b) {
            return ResultTool.fail(201, "删除失败");
        }
        return ResultTool.success("删除成功");
    }

    @CacheEvict(cacheNames = "ParkingUse", allEntries = true)
    public JsonResult updateParkingUse(ParkingUse parkingUse) {
        UpdateWrapper<ParkingUse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", parkingUse.getId());
        updateWrapper.set("community_id", parkingUse.getCommunityId());
        updateWrapper.set("community_name", parkingUse.getCommunityName());
        updateWrapper.set("code", parkingUse.getCode());
        updateWrapper.set("car_number", parkingUse.getCarNumber());
        updateWrapper.set("picture", parkingUse.getPicture());
        updateWrapper.set("owner_name", parkingUse.getOwnerName());
        updateWrapper.set("owner_id", parkingUse.getOwnerId());
        updateWrapper.set("telephone", parkingUse.getTelephone());
        updateWrapper.set("use_type", parkingUse.getUseType());
        updateWrapper.set("total_fee", parkingUse.getTotalFee());
        updateWrapper.set("update_time", parkingUse.getUpdateTime());
        updateWrapper.set("start_time", parkingUse.getStartTime());
        updateWrapper.set("end_time", parkingUse.getEndTime());
        int i = parkingUseMapper.update(updateWrapper);
        if (i <= 0) {
            return ResultTool.fail(201, "更新失败");
        }
        return ResultTool.success("更新成功");
    }
}
